package com.jinwei.qunguanjia.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.common.constant.UserConstants;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.user.dao.GroupCommentReplyDAO;
import com.jinwei.qunguanjia.user.dto.GroupCommentReplyDTO;
import com.jinwei.qunguanjia.user.po.GroupCommentReplyPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("groupCommentReplyService")
public class GroupCommentReplyServiceImpl implements GroupCommentReplyService {

    @Resource
    private GroupCommentReplyDAO groupCommentReplyDAO;

    @Override
    public String save(GroupCommentReplyDTO groupCommentReplyDTO) {
        GroupCommentReplyPO groupCommentReplyPO = new GroupCommentReplyPO();
        BeanUtils.copyProperties(groupCommentReplyDTO, groupCommentReplyPO);
        String groupCommentReplyId = IdUtils.getWorkerId();
        groupCommentReplyPO.setGroupCommentReplyId(groupCommentReplyId);
        groupCommentReplyPO.setCreateTime(DateUtils.getDateTime());
        groupCommentReplyDAO.insert(groupCommentReplyPO);
        return groupCommentReplyId;
    }

    @Override
    public void remove(String id) {
        groupCommentReplyDAO.deleteById(id);
    }

    @Override
    public void update(GroupCommentReplyDTO groupCommentReplyDTO) {
        GroupCommentReplyPO groupCommentReplyPO = new GroupCommentReplyPO();
        BeanUtils.copyProperties(groupCommentReplyDTO, groupCommentReplyPO);
        groupCommentReplyDAO.updateById(groupCommentReplyPO);
    }

    @Override
    public GroupCommentReplyDTO get(String id) {
        GroupCommentReplyPO groupCommentReplyPO = groupCommentReplyDAO.selectById(id);
        if (null == groupCommentReplyPO) {
            return null;
        }
        GroupCommentReplyDTO groupCommentReplyDTO = new GroupCommentReplyDTO();
        BeanUtils.copyProperties(groupCommentReplyPO, groupCommentReplyDTO);
        return groupCommentReplyDTO;
    }

    @Override
    public GroupCommentReplyDTO get(GroupCommentReplyDTO groupCommentReplyDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupCommentReplyDTO);
        // 拼接查询条件
        QueryWrapper<GroupCommentReplyPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        GroupCommentReplyPO groupCommentReplyPO = groupCommentReplyDAO.selectOne(queryWrapper);
        if (null == groupCommentReplyPO) {
            return null;
        }
        BeanUtils.copyProperties(groupCommentReplyPO, groupCommentReplyDTO);
        return groupCommentReplyDTO;
    }

    @Override
    public Long count(GroupCommentReplyDTO groupCommentReplyDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(groupCommentReplyDTO);
        // 拼接查询条件
        QueryWrapper<GroupCommentReplyPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return groupCommentReplyDAO.selectCount(queryWrapper);
    }

    @Override
    public List<GroupCommentReplyDTO> list(GroupCommentReplyDTO groupCommentReplyDTO) throws IllegalAccessException {
        return groupCommentReplyDAO.list(groupCommentReplyDTO);
    }

    @Override
    public PageDataInfo<GroupCommentReplyDTO> listPage(GroupCommentReplyDTO groupCommentReplyDTO) {
        List<GroupCommentReplyDTO> list = groupCommentReplyDAO.list(groupCommentReplyDTO);
        PageInfo<GroupCommentReplyDTO> pageInfo = new PageInfo<>(list);
        PageDataInfo<GroupCommentReplyDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(list);
        pageDataInfo.setTotal(pageInfo.getTotal());
        pageDataInfo.setTotalPage(PageCountUtils.getPageCount(pageInfo.getTotal(), pageInfo.getPageSize()));
        return pageDataInfo;
    }

    @Override
    public String checkUnique(GroupCommentReplyDTO groupCommentReplyDTO) throws IllegalAccessException {
        String id = StringUtils.isNull(groupCommentReplyDTO.getGroupCommentReplyId()) ? "-1" : groupCommentReplyDTO.getGroupCommentReplyId();
        GroupCommentReplyDTO info = get(groupCommentReplyDTO);
        if (StringUtils.isNotNull(info) && !info.getGroupId().equals(id)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    
}
